package com.wt.struggle.goodbye.user.activity

import android.os.Bundle
import android.os.Message
import android.view.View
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.xin.lv.yang.utils.net.Time
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.register_or_forget_layout.*
import org.json.JSONObject

/**
 * 忘记密码
 */
class ForgetActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            222 -> {
                val num = arg1
                if (num > 0) {
                    btRegisterGetYan.text = "$num S"
                } else {
                    btRegisterGetYan.isClickable = true
                    btRegisterGetYan.text = "重新获取"
                }
            }

            Send.SEND_YAN_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    showToastShort("验证码发送成功，请查收")
                }


            }

            Send.FORGET_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    showToastShort("找回成功，请重新登录")
                    finish()
                }

            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.forget_layout)
        tvTitle.text = "忘记密码"

        btRegister.text = "找回密码"
        etRegisterPwdAgain.visibility = View.VISIBLE
        viewLine.visibility = View.VISIBLE

        initClick()
    }

    private fun initClick() {
        imageBack.setOnClickListener {
            finish()
        }

        btRegisterGetYan.setOnClickListener {
            getForgetYan()

        }


        btRegister.setOnClickListener {
            getForgetPwd()
        }

    }

    /**
     * 找回密码
     */
    private fun getForgetPwd() {
        val phone = etRegisterPhone.text.toString()
        val yanCode = etRegisterYan.text.toString()
        val pwd = etRegisterPwd.text.toString()
        val pwdAgain = etRegisterPwdAgain.text.toString()
        when {
            phone == "" -> showToastShort("请输入电话号码")
            yanCode == "" -> showToastShort("请输入获取的验证码")
            pwd == "" -> showToastShort("请输入密码")
            pwdAgain == "" -> showToastShort("请确认密码")
            pwd != pwdAgain -> showToastShort("两次密码输入不同")
            else -> {
                send!!.getForgetPwd(phone, yanCode, pwd, handler!!)
            }
        }
    }


    /**
     * 获取验证码
     */
    private fun getForgetYan() {
        val phone = etRegisterPhone.text.toString()
        if (phone != "") {
            btRegisterGetYan.isClickable = false
            Time(60, 222, handler).start()
            send!!.sendYan(phone, 3, handler!!)
        } else {
            showToastShort("请输入电话号码")
        }
    }
}